package controllers

import (
	"map/libs"
	"map/models"
)

type IndexController struct {
	BaseController
}

type location struct {
	Lat float64 `json:"lat"`
	Lng float64 `json:"lng"`
}
type results struct {
	Name     string   `json:"name"`
	Uid      string   `json:"uid"`
	Location location `json:"location"`
	Address  string   `json:"address"`
	Province string   `json:"province"`
	City     string   `json:"city"`
	Area     string   `json:"area"`
	Aetail   int      `json:"detail"`
}

type Poi struct {
	Message string    `json:"message"`
	Status  int       `json:"status"`
	Results []results `json:"results"`
}

type Address struct {
	Name    string  `json:"name"`
	Address string  `json:"address"`
	Lat     float64 `json:"lat"`
	Lng     float64 `json:"lng"`
}

func (this *IndexController) GetPoi() {
	poi_j, err := libs.GetPoi(this.GetString("address"))
	if err != nil {
		this.errors(poi_j, 1)
	}
	var poi Poi
	err = libs.Str2Map(poi_j, &poi)
	list := make([]Address, len(poi.Results))
	for k, v := range poi.Results {
		list[k] = Address{
			Name:    v.Name,
			Address: v.Address,
			Lat:     v.Location.Lat,
			Lng:     v.Location.Lng}
	}
	this.successData("", list)
}

func (this *IndexController) Config(){
	list,_:=models.ConfigAllList()

	config:=make(map[string]string)
	for _, v := range list {
		config[v.Field]=v.Value
	}

	this.successData("",config)
}